<?php
/**
 * 朱淑浩
 * 小程序直播
 */
namespace App\Http\Controllers\api;

use App\Helpers\Image;
use App\Http\Controllers\Controller;
use App\Models\Live;

use Illuminate\Http\Request;

class LiveManagement extends Controller
{
    /**
     * 直播管理
     */
    public function LiveList()
    {
        $Live_list=Live::paginate(5);
        return view('live',['list'=>$Live_list]);
    }
    public function Mklive()
    {
        return view('mklive');
    }

    /**
     * @param Request $request
     * 创建直播
     */
    public function AddLive(Request $request)
    {

        $param=$request->post();
        $file = $request->file('cover');
        $msg = (new Image())->uploadFile($file);
        unset($param['_token']);
        $param['cover']=$msg;
        $date=date('Y-m-d H:i:s');
        $time=strtotime($date);
        $randStr = str_shuffle('1234567890');
        $rand = substr($randStr,0,6);
        $time=date('Y-m-d H:i:s',strtotime("+1 day",$time));
        $push=$this->getPush('www.txtl.xinyuelog.com',$rand,'580d99b09f25c59eccdcaa2238235bbd',$time);
        $pull=$this->getPullUrl('www.txbf.xinyuelog.com',$rand);
        $param['expired_time']=$time;
        $param['push_url']=$push;
        $param['pull_url']= $pull;
        $param['state']=1;
        $param['start_time']=date('Y-m-d H:i:s');
        $res=Live::create($param);
        if($res)
        {
            return view('live2',['live'=>$res]);
        }
    }
    /**
     * 获取推流地址 webrte
     * 如果不传key和过期时间，将返回不含防盗链的url
     * @param zhu 您用来推流的域名
     *        streamName 您用来区别不同推流地址的唯一流名称
     *        key 安全密钥
     *        time 过期时间 sample 2016-11-12 12:00:00
     * @return String url
     */
    public function getPush($domain,$streamName,$key=null,$time=null)
    {
        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            //txSecret = MD5( KEY + streamName + txTime )
            $txSecret = md5($key.$streamName.$txTime);
            $ext_str = "?".http_build_query(array(
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
        }
        return "webrtc://".$domain."/live/".$streamName . (isset($ext_str) ? $ext_str : "");
    }
    /**
     * @param $domain
     * @param $streamName
     * @param null $key
     * @param null $time
     * @return string
     * 获取播流地址 WebRtc
     */
    public function getPullUrl($domain, $streamName, $key = null, $time = null){
        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            $txSecret = md5($key.$streamName.$txTime);
            $ext_str = "?".http_build_query(array(
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
        }
        return"webrtc://".$domain."/live/".$streamName;
    }

    /**
     * 测试
     */
    public function test()
    {
        return view('play');
    }

    /**
     * 进入直播间
     * @param Request $request
     */
    public function in(Request $request)
    {
        $id=$request->get('id');
        $res=Live::where('id',$id)->first();
        return view('play',['live'=>$res]);
    }
}
